﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Chaos.view.chat
{
    /// <summary>
    /// Interaction logic for chat.xaml
    /// </summary>
    public partial class chat : UserControl
    {

        public class ChatMessageEvent : EventArgs
        {
            public String User { get; set; }
            public String Text { get; set; }
            public ChatMessageEvent()
            {
            }
        };
        public delegate void OnChatMessageDelegate(ChatMessageEvent e);

        public chat()
        {
            InitializeComponent();

            view.manager m = FindResource("view_manager") as view.manager;
            if (m != null)
            {
                m.Controls.Add("chat_control", this);
            }
        }

        public void chat_OnChatMessage(chat.ChatMessageEvent e)
        {
            Paragraph para = new Paragraph(new Run(e.User + ": " + e.Text));
            para.LineHeight = 1;       
            model.game g = FindResource("model_game") as model.game;
            if (g != null)
            {
                model.faction f = g.findfactionbyuser(e.User);
                if (f != null)
                {
                    para.Foreground = new SolidColorBrush(f.Color);
                }
                else
                {
                    para.Foreground = new SolidColorBrush(Colors.Black);
                }
            }

            chatwindow.Document.Blocks.Add(para);
            chatwindow.ScrollToEnd();
        }

        private void chatbox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Return)
            {
                if (chatbox.Text.Length > 0)
                {
                    network.gameclient c = FindResource("game_client") as network.gameclient;
                    if (c != null)
                    {
                        messages.request_chat_text msg = new messages.request_chat_text();
                        msg.Text = chatbox.Text;
                        c.sendmessage(msg);
                        chatbox.Text = "";
                    }
                }
            }
        }
    }
}
